package com.example.date_save_test;

import androidx.appcompat.app.AppCompatActivity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.example.date_save_test.database.UserDbHelper;
import com.example.date_save_test.user.User;
import com.example.date_save_test.util.ToastUtil;

import java.time.LocalDate;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText etName;
    private EditText etAge;
    private CheckBox ckMarried;
    private UserDbHelper userDbHelper;
    private User user = null;
    @Override
    protected void onStart() {
        super.onStart();
        userDbHelper = UserDbHelper.getInstance(this);
        userDbHelper.openWriteConnect();
        userDbHelper.openReadConnect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        userDbHelper.close();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etName = findViewById(R.id.et_name);
        etAge = findViewById(R.id.et_age);
        ckMarried = findViewById(R.id.cK_marry);


        findViewById(R.id.btn_saveData).setOnClickListener(this);
        findViewById(R.id.btn_deleteData).setOnClickListener(this);
        findViewById(R.id.btn_updateData).setOnClickListener(this);
        findViewById(R.id.btn_queryData).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_saveData:
                 String name = etName.getText().toString();
                 String age = etAge.getText().toString();

                user = new User(name,
                        Integer.parseInt(age),
                        ckMarried.isChecked());
                long insert = userDbHelper.insert(user);
                if(insert>0){
                    ToastUtil.successShow(this,"添加成功");
                }

                break;
            case R.id.btn_deleteData:
                long l = userDbHelper.deleteByName(etName.getText().toString());
                if(l >0){
                    ToastUtil.successShow(this,"删除成功");
                }
            case R.id.btn_updateData:
                user = new User(etName.getText().toString(),
                        Integer.parseInt(etAge.getText().toString()),
                        ckMarried.isChecked());
                Integer integer = userDbHelper.updateInfo(user);
                if (integer>0){
                    ToastUtil.successShow(this,"更新成功");
                }
                break;
            case R.id.btn_queryData:
                List<User> queryAll = userDbHelper.queryAll();
                for (User user : queryAll){
                    Log.d("TAG",user.toString());
                }
                break;
        }
    }
}